/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.zosean.sky.stringItem;

import java.util.Hashtable;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Administrator
 */
public class PaintBitMap
{

    private String lastString = "";  // Store the last parse the string
    private int lastSamllBitMapWidth = 0; // Store the last parse the width of bitmap
    private int lastoverallWidth = 0; // Store the last parse the width of source iamge
    private String[] bitMapStringArray = null; // The string array with used in hash table of key
    private int wordsSpace = 5; // Words and word spaceing
    /**
     * Defautlt construction method
     */
    public PaintBitMap()
    {
        
    }

    /**
     * Packing Drawing bitmap method
     * @param g Graphics
     * @param strValue One time to draw string
     * @param ht The hash table is stored bitmap
     * @param x Draw x-coordinate
     * @param y Draw y-coordinate
     */
    public final void bitPaint(Graphics g, String strValue, Hashtable ht, int x, int y)
    {
        String [] tempReciveStringArray = stringToStringArray(strValue);
        int tempLength = tempReciveStringArray.length;
        int tempTheValueImageWidth = 0;
        Image tempImage = null;
        for(int i = 0; i < tempLength; i++)
        {
            if(ht.containsKey(tempReciveStringArray[i]))  // If this string exists in hash table
                tempImage = (Image)ht.get(tempReciveStringArray[i]);
            g.drawImage(tempImage, x, y, 0);
            tempTheValueImageWidth = tempImage.getWidth();
            x += (tempTheValueImageWidth + wordsSpace);
        }
    }


    /**
     * Automatic or manual handling strings wrap
     * @param strValue    One time load the string
     * @param smallBitMapWidth The width of a single string
     * @param width The width of change to the next line
     */
    public final void analysisString(String strValue, int smallBitMapWidth, int width)
    {
        if(smallBitMapWidth > width && !isTheSourceLastAllTheSameCurrent(strValue, smallBitMapWidth, width))
        {
            System.out.println(">> WARNING : " + "The width of a single char more than overall width");
            bitMapStringArray = null;
            return;
        }
        if(isTheSourceLastAllTheSameCurrent(strValue, smallBitMapWidth, width))
        {
//            System.out.println(">>WARNING = " + " And the last strings widthout having to parse");
            return;
        }
        lastString = strValue;
        lastSamllBitMapWidth = smallBitMapWidth;
        lastoverallWidth = width;
        int currentIndex = 0;
        Vector vc = new Vector();
        StringBuffer sb = new StringBuffer();
        int currentWidth = 0;
        while (currentIndex < strValue.length())
        {
//            System.out.println(currentIndex);
            currentWidth += smallBitMapWidth;
            if (currentWidth > width)
            {
                vc.addElement(sb.toString());
                currentWidth = 0;
                sb = new StringBuffer();
//                System.out.println(" > width");
            } else if (strValue.charAt(currentIndex) == '|')
            {
                if(sb.length() > 0)
                    vc.addElement(sb.toString());
                currentWidth = 0;
                currentIndex++;
                sb = new StringBuffer();
//                System.out.println("|");
            } else
            {
                sb.append(strValue.charAt(currentIndex));
                currentIndex++;
//                System.out.println("++");
            }
        }
        if (currentIndex >= strValue.length() && sb.length() > 0)
        {
            vc.addElement(sb.toString());
        }
        bitMapStringArray = new String[vc.size()];
        for (int i = 0; i < bitMapStringArray.length; i++)
        {
            bitMapStringArray[i] = (String) vc.elementAt(i);
        }
        // Manually empty the local variables
        if(sb != null)
            sb = null;
        if(vc != null)
            vc = null;
    }

    /**
     * 
     * @return Back the analysisString method results
     */
    public final String[] getBitMapStringArray()
    {
        if (bitMapStringArray != null)
        {
            return bitMapStringArray;
        } else
        {
            return null;
        }
    }

    /**
     * Comparative analysis of the string of the two are identical
     * @param sourceString   One time load the string
     * @param bitMapWidth    The width of a single string
     * @param width          The width of change of the next line
     * @return               Back to whether fully equal
     */
    private final boolean isTheSourceLastAllTheSameCurrent(String sourceString, int bitMapWidth, int width)
    {
        return (sourceString.equals(lastString) && bitMapWidth == lastSamllBitMapWidth && width == lastoverallWidth);
    }

    /**
     * Converted to a string array of strings
     * Used in the hash table of key
     * @param strValue    To draw the string
     * @return            Return a string array
     */
    public final String[] stringToStringArray(String strValue)
    {

        if(strValue != null)
        {
//            char[] tempCharArray = new char[strValue.length()];
            String [] stringArray = new String[strValue.length()];
            int tempLength = stringArray.length;
            for(int i = 0; i < tempLength; i++)
            {
                stringArray[i] = String.valueOf(strValue.charAt(i));
            }
            return stringArray;
        }
        return null;
    }

    /**
     * Setting the words and word space
     * @param value
     */
    public void setWrodsSpace(int value)
    {
        if(value < 0)
        {
            System.out.println(">>WARNING : " + "The wrods spaceing value less than zero");
            value = Math.abs(value);
        }
        wordsSpace = value;
    }

    /**
     * Get the words and word space
     * @return wordsSpace
     */
    public int getWordsSpace()
    {
        return wordsSpace;
    }
}
